package com.core.db.execute;

import java.sql.SQLException;
import java.util.List;

import com.core.db.condition.Condition;
import com.core.db.require.Assembler;
import com.core.db.require.Require;

/**
 * 持久层
 * 
 * @author 殷田
 *
 */
public class Dao
{
	/**
	 * 查询操作
	 * 
	 * @param clazz
	 * @param cond
	 * @return
	 * @throws SQLException
	 */
	public static < T > List< T > query( Class< T > clazz, Condition cond ) throws SQLException
	{
		Require require = Assembler.query( clazz, cond );
		Result result = new Executor().query( require );
		List< T > values = Assembler.parseResult( result.getResultSet(),
						require.getColumnDescribes(), clazz );
		result.release();
		return values;
	}

	/**
	 * 插入操作
	 * 
	 * @param obj
	 * @return
	 * @throws SQLException
	 */
	public static boolean insert( Object obj ) throws SQLException
	{
		Require require = Assembler.insert( obj );
		return new Executor().insert( require );
	}

	/**
	 * 删除操作
	 * 
	 * @param clazz
	 * @param cond
	 * @return
	 * @throws SQLException
	 */
	public static boolean delect( Class< ? > clazz, Condition cond ) throws SQLException
	{
		Require require = Assembler.delect( clazz, cond );
		return new Executor().delect( require );
	}

	/**
	 * 更新操作
	 * 
	 * @param obj
	 * @param cond
	 * @return
	 * @throws SQLException
	 */
	public static boolean update( Object obj, Condition cond ) throws SQLException
	{
		Require require = Assembler.update( obj, cond );
		return new Executor().update( require );
	}

	/**
	 * 直接执行查询sql
	 * 
	 * @param table
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	public static Result rawQuery( String sql ) throws SQLException
	{
		return new Executor().rawQuery( sql );
	}

	/**
	 * 直接执行sql语句
	 * 
	 * @param table
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	public static boolean raw( String sql ) throws SQLException
	{
		return new Executor().raw( sql );
	}

}
